﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
using WLMCoreLibrary;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace MiWebSite_Core
{
    [Serializable]
    public class MiWebSitePage
    {
        [XmlArrayItem("script")]
        public List<string> Scripts
        {
           get;set;
        
        }

        [XmlArrayItem( "styleSheet" )]
        public List<string> StyleSheets
        {
           get;set;
           
        }

        [XmlElement("JSCommandBefore")]
        public List<string> JSCommandBefore
        {
           get;set;
          
        }

        [XmlElement("JSCommandAfter")]
        public List<string> JSCommandAfter
        {
           get;set;
         
        }

        [XmlAttribute()]
        public string Title
        {
           get;set;
            
        }

        [XmlAttribute()]
        public string Keywords
        {
           get;set;
     
        }

        [XmlAttribute()]
        public string DomainName
        {
           get;set;
         
        }

        [XmlAttribute()]
        public int DomainID
        {
           get;set;
   
        }

        [XmlAttribute()]
        public string Description
        {
           get;set;

        }

        public Categoria Navigation
        {
           get;set;
 
        }

     
        public XmlNode AditionalContent
        {
           get;set;
   
        }

        public XmlNode SideContent
        {
           get;set;
  
        }

        public string Estadisticas
        {
           get;set;
 
        }

        public XmlNode Contenido
        {
           get;set;
          
        }

        public string Logeduser
        {
           get;set;
        
        }



        public MiWebSitePage()
        {
            Scripts = new List<string>();
            StyleSheets = new List<string>();
        }

        public MiWebSitePage(ref dbConnector ConexionDB)
        {
            Scripts = new List<string>();
            StyleSheets = new List<string>();
            JSCommandAfter = new List<string>();
            JSCommandBefore = new List<string>();
            WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
            Navigation = new Categoria(1, "", ref ConexionDB); ;
            //FootNavigation = new Categoria(1, "", ref ConexionDB);
            //StoreNavigation = new Categoria(6, "/" + config.Settings["StorePathCategoria"].value, ref ConexionDB);
            //if (CurrentUser.IdUsuario != 0)
            //    FootNavigation.Items.Add(new MiWebSite_Core.Categoria("LogOut", "/login?logout=true", 9999));
            //else
            //    FootNavigation.Items.Add(new MiWebSite_Core.Categoria("¥", "/login", 9999));
            Title = config.Settings["siteName"].value;
            Estadisticas = CurrentDomain.estadisticas;
            Keywords = CurrentDomain.DomainKeywords;

        }


    }

    public class MiWebSiteTemplate : BaseClass
    {


        #region public properties
        public String nombre {get;set; }
        public String template {get;set; }
        #endregion

        #region constructors
        public MiWebSiteTemplate(ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("templates");
            string queryString = "";
            queryString = "SELECT * FROM templates WHERE id=\"" + id + "\"";
            GetRecord(queryString);
        }

        public MiWebSiteTemplate(string nombre, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM templates WHERE nombre='" + nombre + "'";
            GetRecord(queryString);
        }

        public MiWebSiteTemplate(int registryId, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM templates WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }
        #endregion

        #region public methods


        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM templates WHERE id=\"" + id + "\"";
            ConexionDBInterna.ExecuteCommand(queryString);

        }
        #endregion

        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                id = (System.Int32)reader["id"];
                fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]); if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];

                idUsuario = (System.Int32)reader["idUsuario"];
                nombre = Convert.ToString(reader["nombre"]);
                template = Convert.ToString(reader["template"]);
            }
        }


        protected override void UpdateBind()
        {
            template = WLMFunctions.comillas(template);

            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE templates set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'"); strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");
            strCommand.Append("fechaModificacion='" + DateTime.UtcNow.AddHours(-6).ToString("yyyy-MM-dd HH:mm:ss") + "'");
            strCommand.Append(",nombre=\"" + nombre.ToString() + "\"");
            strCommand.Append(",template=\"" + template.ToString() + "\"");
            strCommand.Append(" WHERE id=\"" + id + "\"");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
            HttpContext.Current.Application.Clear();
        }
        #endregion
    }

    public class MiWebSiteTemplateCollection
    {

        public List<MiWebSiteTemplate> Templates { get; set; }
        public MiWebSiteTemplateCollection(ref dbConnector ConexionDB)
        {
            string strCommand = "SELECT id FROM templates order by id desc";
            OdbcDataReader reader = ConexionDB.ExecuteReader(strCommand);
            Templates = new List<MiWebSiteTemplate>();
            while (reader.Read())
            {
                MiWebSiteTemplate newEntry = new MiWebSiteTemplate(Convert.ToInt32(reader["id"]), ref ConexionDB);

                Templates.Add(newEntry);
            }
            reader.Close();
            reader.Dispose();
        }
    }



}
